/* store.js */
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export function CreateStore () {
  console.log('-----CreateStore 开始--------------')
  let store = new Vuex.Store({
    state: {
      homeInfo: '',
      mode: '',
      movies: []
    },
    actions: {
      getHomeInfo ({ commit }) {
        return axios.get('http://localhost:10016/api/getHomeInfo').then((res) => {
          commit('setHomeInfo', res.data)
        })
      },
      getMovies ({ commit }) {
        return axios.get('https://api.douban.com/v2/movie/top250?count=50').then((res) => {
          commit('setMovies', res.data.subjects)
        })
      }
    },
    mutations: {
      setHomeInfo (state, res) {
        state.homeInfo = res
      },
      setMovies (state, res) {
        state.movies = res
        console.log('JSON.stringify(state.movies) ：' + JSON.stringify(state.movies))
      }
    }
  })
  console.log('--------CreateStore 结束 即将返回store--------------')
  console.log('store.js:    store' + store)
  return store
}
